// pages/authorize/index.js
const util = require('../../utils/util.js');
const api = require('../../config/api.js');
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userData: {
      'encryptedData': '',
      'iv': '',
      'signature': '',
      'rawData': '',
      'code': ''
    },
    iswork:false,
  
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    let that =  this;
    console.log("xiala");
    wx:wx.stopPullDownRefresh(); //jieshu
    let isstart = that.data.iswork;
    isstart = !isstart;
    this.setData({
      iswork: isstart
    })
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  bindGetUserInfo: function (e) {
    console.log(e);
    if (!e.detail.userInfo) {
      return;
    }
    let userinfo = e.detail.userInfo;
    //wx.setStorageSync('userInfo', e.detail.userInfo)

    console.log(e);
    app.globalData.userInfo = e.detail.userInfo

    let user = this.data.userData;
    user.encryptedData = e.detail.encryptedData;
    user.iv = e.detail.iv;
    user.signature = e.detail.signature;
    user.rawData = e.detail.rawData;
    let that = this;
    // // 登录
    wx.login({
      success: res => {
        console.log(res);
        console.log(res.data);
        user.code = res.code;
        that.setData({
          userInfo: e.detail.userInfo,
          userData: user,
          hasUserInfo: true
        });



        util.request(api.AuthLoginByWeixin, that.data.userData, 'POST').then(function (res) {
          console.log(res);
          if (res.code == 0) {
            console.log(res.data.userid);
            wx.setStorageSync('userids', res.data.userid);
            wx.setStorageSync('userinfo', userinfo);
            wx.setStorageSync('usertype', 1);
            wx.navigateBack();
            // wx.navigateTo({
            //   url: '../orderlist/orderlist'
            // })
          }
          else{
            util.showErrorToast("登陆失败");
          }
        });
      }
    })


    
    // wx.getStorage({
    //   key: 'code',
    //   success: function (res) {
    //     console.log(res.data);
    //     user.code = res.data;
    //     that.setData({
    //       userInfo: e.detail.userInfo,
    //       userData: user,
    //       hasUserInfo: true
    //     });



    //     util.request(api.AuthLoginByWeixin, that.data.userData, 'POST').then(function (res) {
    //       console.log(res);
    //       if (res.code == 0) {
    //         // console.log(res.data.userid);
    //         // wx.setStorageSync('userids', res.data.userid);
    //         // wx.navigateTo({
    //         //   url: '../orderlist/orderlist'
    //         // })
    //       }
    //     });
    //   }
    // })
    //this.login();
  },
  bindGetUserInfo_w: function (e) {
    console.log(e);
    if (!e.detail.userInfo) {
      return;
    }
    wx.setStorageSync('userinfo', e.detail.userInfo);
    wx.setStorageSync('usertype', 2);
    wx.navigateBack();
    //this.login();
  },


})